/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.sh.ndt.question.dao;

import com.sh.ndt.common.DBObjects;
import com.sh.ndt.common.dao.BaseDAO;
import com.sh.ndt.common.dao.ExtractDataRows;
import com.sh.ndt.exception.DAOException;
import com.sh.ndt.question.mapper.QuestionMapper;
import com.sh.ndt.question.entity.Question;
import java.sql.Types;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.apache.log4j.Logger;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.SqlOutParameter;
import org.springframework.jdbc.core.SqlParameter;
import org.springframework.jdbc.core.SqlReturnResultSet;
import org.springframework.jdbc.core.support.JdbcDaoSupport;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;

/**
 *
 * @author Prashanth
 */
public class QuestionDAOImpl extends JdbcDaoSupport implements QuestionDAO {

    private static final Logger LOGGER = Logger.getLogger(QuestionDAOImpl.class);

    private QuestionDAOImpl(){
        LOGGER.debug("****************** QuestionDAOImpl Initialized **");
    }
    
    @Override
    public List<Question> getQuestions(long categoryId) throws DAOException {

        LOGGER.debug("Inside QuestionDAOImpl's getQuestions method...");

        try {
            SqlParameter sqlParam[] = new SqlParameter[2];

            sqlParam[0] = new SqlParameter("in_category_id", Types.INTEGER);
            sqlParam[1] = new SqlReturnResultSet("QUESTION_LIST", new QuestionMapper());

            ExtractDataRows rows = new ExtractDataRows(getJdbcTemplate().getDataSource(), DBObjects.SP_GET_QUESTIONS_ON_COURSE, sqlParam);
            HashMap inputs = new HashMap();
            inputs.put("in_category_id", categoryId);

            Map results = rows.executeStoredProcedure(inputs);
            List<Question> questionList = (List<Question>) results.get("QUESTION_LIST");
            
            LOGGER.debug("Question Retrival was successful: " + questionList.size());

            return questionList;
        } catch (DAOException e) {
            LOGGER.debug(e);
        }
        return null;
    }

    @Override
    public List<Question> getQuestionsOnRowcount(long categoryId, int rowCount) throws DAOException {

        LOGGER.debug("Inside QuestionDAOImpl's getQuestions method...");

        boolean result = false;
        try {
            SqlParameter sqlParam[] = new SqlParameter[3];

            sqlParam[0] = new SqlParameter("in_category_id", Types.INTEGER);
            sqlParam[1] = new SqlParameter("in_rowcount", Types.INTEGER);
            sqlParam[2] = new SqlReturnResultSet("QUESTION_LIST", new QuestionMapper());

            ExtractDataRows rows = new ExtractDataRows(getJdbcTemplate().getDataSource(), DBObjects.SP_GET_QUESTIONS_ON_ROWCOUNT, sqlParam);
            HashMap inputs = new HashMap();
            inputs.put("in_category_id", categoryId);
            inputs.put("in_rowcount", rowCount);

            Map results = rows.executeStoredProcedure(inputs);
            List<Question> questionList = (List<Question>) results.get("QUESTION_LIST");
            
            LOGGER.debug("Question Retrival was successful: " + questionList.size());

            return questionList;
        } catch (DAOException e) {
            LOGGER.debug(e);
        }
        return null;
    }    

    @Override
    public String markAsFailed(long scheduleId, long scheduleStatus) throws DAOException {
        
        LOGGER.debug("Inside QuestionDAOImpl's markAsFailed method...");

       
        try {
            SqlParameter sqlParam[] = new SqlParameter[3];

            sqlParam[0] = new SqlParameter("in_schedule_id", Types.INTEGER);        
            sqlParam[1] = new SqlParameter("in_status", Types.INTEGER);        
            sqlParam[2] = new SqlOutParameter("OUT_SP_MESSAGE", Types.VARCHAR);
            
            ExtractDataRows rows = new ExtractDataRows(getJdbcTemplate().getDataSource(), DBObjects.SP_EDIT_EXAM_SCHEDULE, sqlParam);
            HashMap inputs = new HashMap();
            inputs.put("in_schedule_id", scheduleId);
            inputs.put("in_status", scheduleStatus);
            
            Map results = rows.executeStoredProcedure(inputs);

            if (results.get("OUT_SP_MESSAGE") != null) {
                return (String)results.get("OUT_SP_MESSAGE");
            }            
            
        } catch (DAOException e) {
            LOGGER.debug(e);
        }
        
        return null;
    }
    
    @Override
    public String markAsPassed(long scheduleId, long scheduleStatus) throws DAOException {
        
        LOGGER.debug("Inside QuestionDAOImpl's markAsPassed method...");

       
        try {
            SqlParameter sqlParam[] = new SqlParameter[3];

            sqlParam[0] = new SqlParameter("in_schedule_id", Types.INTEGER);        
            sqlParam[1] = new SqlParameter("in_status", Types.INTEGER);        
            sqlParam[2] = new SqlOutParameter("OUT_SP_MESSAGE", Types.VARCHAR);
            
            ExtractDataRows rows = new ExtractDataRows(getJdbcTemplate().getDataSource(), DBObjects.SP_EDIT_EXAM_SCHEDULE, sqlParam);
            HashMap inputs = new HashMap();
            inputs.put("in_schedule_id", scheduleId);
            inputs.put("in_status", scheduleStatus);
            
            Map results = rows.executeStoredProcedure(inputs);

            if (results.get("OUT_SP_MESSAGE") != null) {
                return (String)results.get("OUT_SP_MESSAGE");
            }            
            
        } catch (DAOException e) {
            LOGGER.debug(e);
        }
        
        return null;
    }    

    @Override
    public String updateMockSession(long userId, long testId, int result, int creditPoint) throws DAOException {
 LOGGER.debug("Inside QuestionDAOImpl's updateMockSession method...");

       
        try {
            SqlParameter sqlParam[] = new SqlParameter[5];

            sqlParam[0] = new SqlParameter("IN_USER_ID", Types.INTEGER);        
            sqlParam[1] = new SqlParameter("IN_EXAM_ID", Types.INTEGER);        
            sqlParam[2] = new SqlParameter("IN_RESULT", Types.INTEGER);
            sqlParam[3] = new SqlParameter("IN_CREDIT_POINT", Types.INTEGER);
            sqlParam[4] = new SqlParameter("IN_PERCENTAGE_SECURED", Types.DECIMAL);
            sqlParam[5] = new SqlOutParameter("OUT_SP_MESSAGE", Types.VARCHAR);
            
            ExtractDataRows rows = new ExtractDataRows(getJdbcTemplate().getDataSource(), DBObjects.SP_ADD_TEST_HISTORY, sqlParam);
            HashMap inputs = new HashMap();
            
            inputs.put("IN_USER_ID", userId);
            inputs.put("IN_EXAM_ID", testId);
            inputs.put("IN_RESULT", result);
            inputs.put("IN_CREDIT_POINT", creditPoint);
            
            Map results = rows.executeStoredProcedure(inputs);

            if (results.get("OUT_SP_MESSAGE") != null) {
                return (String)results.get("OUT_SP_MESSAGE");
            }            
            
        } catch (DAOException e) {
            LOGGER.debug(e);
        }
        
        return null;    }
}
